<?php

namespace App\Http\Imports\BaseData;

use App\Models\Dic;
use App\Models\MaterialCategory;
use App\Models\Material;
use DB;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class MaterialImport implements ToCollection
    {
    /**
     * @param Collection $collection
     */
    public function collection(Collection $collection)
        {
        set_time_limit(0);
        $collection->filter(function ($v, $k)
            {
            if ($k == 0) {
                return;
                }
            $category_name = $v[0];
            $categ_m       = MaterialCategory::whereName($category_name)->first();
            if ($categ_m) {
                $category_id = $categ_m->id;
                }
            else {
                fail("第" . $k . "{$category_name}分组不存在！");
                }
            $code     = $v[1];
            $material = Material::whereCode($code)->first();
            if ($material) {
                fail("第" . $k . "{$code}编码已存在不存在！");
                }
            $name              = $v[2];
            $fullname          = $v[3];
            $spec              = $v[4] ?? "";
            $comp              = $v[5] ?? "";
            $functions         = $v[6];
            $show              = $v[7];
            $storage_unit_name = $v[8] == "千克" ? "Kg" : $v[8];
            $dic_m             = Dic::whereCode("unit")->whereName($storage_unit_name)->first();

            if ($dic_m) {
                $ounit_id = $dic_m->key;
                }
            else {
                fail("第" . $k . "{$storage_unit_name}单位不存在！");
                }
            $remark           = $v[9];
            $film_c_name      = $v[10];
            DB::enableQueryLog();
            $film_m           = Dic::whereCode("FilmCategory")->whereName($film_c_name)->first();
          
            $film_category_id = NULL;
            if ($film_m) {
                $film_category_id = $film_m->key;
                }
            else {
                dd(db::getQueryLog());
                fail("第" . $k . ",膜类型【{$film_c_name}】不存在！");
                }
            $flower_pattern = $v[11];
            $width          = $v[12];
            $weight         = $v[13];
            $customer_allow = $v[14] == "是" ? "1" : "0";
            $purchase_allow = $v[15] == "是" ? "1" : "0";
            $pro_allow      = $v[16] == "是" ? "1" : "0";

            $purchse_uunit_name = $v[17] == "千克" ? "Kg" : $v[17];
            $dic_m              = Dic::whereCode("unit")->whereName($purchse_uunit_name)->first();

            if ($dic_m) {
                $uunit_id = $dic_m->key;
                }
            else {
                fail("第" . $k . "{$purchse_uunit_name}单位不存在！");
                }

            $coefficient = $v[18];
            $batch       = $v[19];
            if ($category_id == 273) {
                if (!is_numeric($spec) || $spec <= 0) {
                    fail("第" . $k . "{$fullname}规格不正确！必须是数字！");
                    }
                $weight = $spec * 1.2;
                }

            if ($uunit_id != $ounit_id) {
                if ($category_id == 273) {
                    $coefficient = 1000 / 1.65 / $weight / 1000;
                    }
                else {
                    $coefficient = 1000 / 1.56 / $weight / 1000;
                    }
                }

            Material::create([
                'category_id'      => $category_id,
                'code'             => $code,
                'name'             => $name,
                'fullname'         => $fullname,
                'spec'             => $spec,
                'comp'             => $comp,
                'functions'        => $functions,
                'show'             => $show,
                'uunit_id'         => $uunit_id,
                'ounit_id'         => $ounit_id,
                'remark'           => $remark,
                'film_category_id' => $film_category_id,
                'flower_pattern'   => $flower_pattern,
                'width'            => $width,
                'weight'           => $weight,
                'customer_allow'   => $customer_allow,
                'purchase_allow'   => $purchase_allow,
                'pro_allow'        => $pro_allow,
                'coefficient'      => $coefficient,
                'batch'            => $batch,
            ]);
            });

        }
    }
